package com.ford.trade.bigCompany;

import com.ford.trade.entity.Game;
import com.ford.trade.entity.PeiLv2;
import com.ford.trade.service.Game500Service;
import lombok.Data;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: ford
 * @date: 2022/4/23 13:41
 */
@Data
@Component
public class Peilv2Processor implements PageProcessor {

    private Peilv2Service peilv2Service;
    private Game500Service game500Service;

    private String type;
    private String date;
    private String cookie;
    private List<Game> games;

    @Override
    public void process(Page page) {
        PeiLv2 peiLv = new PeiLv2();
        Document document = page.getHtml().getDocument();
        String url = page.getUrl().get();
        //获取欧赔
        String companyType = "";
        Long indexId = Long.valueOf(url.substring(url.lastIndexOf("-") + 1, url.lastIndexOf(".")));
        if ("oupei".equals(type)) {
            int length = url.length();
            if (url.contains("order")) {
                if ("0".equals(url.substring(length - 1))) {
                    companyType = "大公司";
                }
            }
            games.forEach(game -> {
                if (game.getIndexId().equals(indexId)) {
                    peiLv.setType(game.getType());
                    peiLv.setDate(game.getDate());
                    peiLv.setZhuDui(game.getZhuDui());
                    peiLv.setRangQiu(game.getRangQiu());
                    peiLv.setKeDui(game.getKeDui());
                    peiLv.setResult(game.getResult());
                }
            });
            peiLv.setIndexId(indexId);
            peiLv.setCompanyType(companyType);
            //初始胜
            peiLv.setSheng(Double.valueOf(document.getElementById("avwinc2").text()));
            //初始平
            peiLv.setPing(Double.valueOf(document.getElementById("avdrawc2").text()));
            //初始负
            peiLv.setFu(Double.valueOf(document.getElementById("avlostc2").text()));
            //变化胜
            peiLv.setShengChange(Double.valueOf(document.getElementById("avwinj2").text()));
            //变化平
            peiLv.setPingChange(Double.valueOf(document.getElementById("avdrawj2").text()));
            //变化负
            peiLv.setFuChange(Double.valueOf(document.getElementById("avlostj2").text()));
            //返还率
            peiLv.setReturnRate(document.getElementById("avpaylc2").text());
            //初始胜凯利
            peiLv.setShengKelly(Double.valueOf(document.getElementById("avklwc2").text()));
            //初始平凯利
            peiLv.setPingKelly(Double.valueOf(document.getElementById("avkldc2").text()));
            //初始负凯利
            peiLv.setFuKelly(Double.valueOf(document.getElementById("avkllc2").text()));

            Integer returnRate = Integer.valueOf(document.getElementById("avpaylc2").text().substring(0, 2));
            Integer sheng = Integer.valueOf(document.getElementById("avklwc2").text().replace(".", ""));
            Integer ping = Integer.valueOf(document.getElementById("avkldc2").text().replace(".", ""));
            Integer fu = Integer.valueOf(document.getElementById("avkllc2").text().replace(".", ""));
            peiLv.setChazhi(Math.abs(returnRate - sheng) + "-" + Math.abs(returnRate - ping) + "-" + Math.abs(returnRate - fu));
            peiLv.setDatetime(new Date());
            peiLv.setYaChu("");
            peiLv.setYaChange("");
            peiLv.setDiscrete(document.getElementById("lswc2").text()+"-" +document.getElementById("lsdc2").text()+ "-"+document.getElementById("lslc2").text());
            peiLv.setDiscreteChange(document.getElementById("lswj2").text()+"-" +document.getElementById("lsdj2").text()+ "-"+document.getElementById("lslj2").text());

            peilv2Service.save(peiLv);
        } else if ("yapei".equals(type)) {
            List<PeiLv2> peiLv2s = peilv2Service.findByIndexId(indexId);
            if (!CollectionUtils.isEmpty(peiLv2s)) {
                List<PeiLv2> collect = peiLv2s.stream()
                        .sorted(Comparator.comparing(PeiLv2::getDatetime).reversed())
                        .collect(Collectors.toList());
                PeiLv2 peiLv2 = collect.get(0);
                Elements elements = document.select("span:contains(Interwetten)");
                if (CollectionUtils.isEmpty(elements)) {
                    return;
                }
                Element element = elements.first().parent().parent().parent().parent();
                Element yaChu = element.getElementsByClass("pl_table_data").last();
                Element yaChange = element.getElementsByClass("pl_table_data").first();
                Elements td = yaChu.getElementsByTag("td");
                String yaChu1 = td.get(0).text() + "-" + td.get(1).text() + "-" + td.get(2).text();
                Elements td2 = yaChange.getElementsByTag("td");
                String yaChu2 = td2.get(0).text() + "-" + td2.get(1).text() + "-" + td2.get(2).text();
                peiLv2.setYaChu(yaChu1);
                peiLv2.setYaChange(yaChu2);
                peilv2Service.save(peiLv2);
            }
        }

    }

    private Site site = Site.me()
            .setCharset("gb2312")
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36")
            .addCookie("cookie", cookie)
            .setTimeOut(3000)
            .setRetrySleepTime(1000)
            .setRetryTimes(3);

    @Override
    public Site getSite() {
        return site;
    }

}
